﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ResetPassword : System.Web.UI.Page
{
    internal string email = "";
    internal string keyData = "";
    public string messagetype = "";
    public string message = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Request.QueryString != null && Request.QueryString["key"] != null)
            {
                keyData = Request.QueryString["key"].ToString();
                email = "   " + new ProxyWrapper.UserServiceWrapper().GetEmail(keyData);
                if (string.IsNullOrWhiteSpace(email))
                {
                    message = "Link expired. Please try to generate new link";
                    messagetype = "E";
                    area.Visible = false;
                }
            }
            else if (Request.Form["email"] != null && Request.Form["key"] != null)
            {
                email = Request.Form["email"].ToString();
                keyData = Request.Form["key"].ToString();
                if (string.IsNullOrWhiteSpace(Request.Form["key"].ToString()))
                {
                    message = "Link expired. Please try to generate new link";
                    messagetype = "E";
                }
                else if (string.IsNullOrWhiteSpace(Request["password1"].ToString()) || string.IsNullOrWhiteSpace(Request["password2"].ToString()))
                {
                    message = "Password fields can not be empty";
                    messagetype = "E";
                }
                else
                {
                    string password = Request["password2"];
                    string key = Request.Form["key"].ToString().Trim();
                    message = new ProxyWrapper.UserServiceWrapper().ResetPassword(key, password);
                    messagetype = "S";
                    if (message == "0")
                    {
                        message = "Unable to reset password. Please contact system administrator";
                        messagetype = "E";
                    }
                    area.Visible = false;
                }
            }
            else
            {
                Response.Redirect("Default.aspx", false);

            }

        }
        catch (Exception ex)
        {
            message = ex.Message;
            messagetype = "E";
        }
    }
    public string GetMessage()
    {
        string warning = "<div class='warning'><p></p><li>{0}</li><p></p></div>";
        string result = "<div class='success'><p></p><li>{0}</li><p></p></div>";
        if (messagetype == "E")
            return string.Format(warning, message);
        else if (messagetype == "S")
            return string.Format(result, message);
        else
            return "";

    }
}